package com.bidv.wcm;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bidv.BO.PaginationBO;
import com.bidv.util.DateUtil;
import com.bidv.util.Pagination;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.ShortTextComponent;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;

/**
 * Servlet implementation class GetGroup
 */
@WebServlet("/GetGroup")
public class GetGroup extends HttpServlet {
	private static final String SO_KY_HIEU = "SoKyHieu";
	private static final String TOTAL = "total";
	private static final String HREF = "href";
	private static final String MODIFIED_DATE = "modifiedDate";
	private static final String LABEL_DOC = "label";
	private static final String DETAIL_JSP_WCM_WPS_WCM_CONNECT = "detail.jsp?wcm=/wps/wcm/connect/";
	private static final String TEMP_VAN_BAN = "TempVanBan";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetGroup() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// bien json
		PrintWriter out = response.getWriter();
		JSONArray jsonArray = new JSONArray();
		JSONObject json = null;
		// TODO Auto-generated method stub
		int startPage = Integer.parseInt(request.getParameter("StartPage"));
		int pageSize = Integer.parseInt(request.getParameter("PageSize"));
		String site = request.getParameter("Site");
		String library = request.getParameter("Library");
		String dir = request.getParameter("Dir");

		try {
			// SiteArea siteArea = null;
			DocumentId siblingId = null;
			DocumentId authoringTmpl = null;
			DocumentId siteArea = null;
			DocumentIdIterator authoringIterator = null;
			DocumentIdIterator siteAreaIterator = null;
			DocumentId<Content> contentid = null;
			int k = 1;

			Workspace myworkspace = WCM_API.getRepository()
					.getSystemWorkspace();
			myworkspace.login();

			DocumentLibrary MyLibrary = myworkspace.getDocumentLibrary(library);
			myworkspace.setCurrentDocumentLibrary(MyLibrary);

			// find Authoring template
			authoringIterator = myworkspace.findByName(
					DocumentTypes.AuthoringTemplate, TEMP_VAN_BAN);
			// get authoring template of lib
			authoringTmpl = authoringIterator.next();
			// find Sitearea
			siteAreaIterator = myworkspace.findByName(DocumentTypes.SiteArea,
					site);
			// get siteArea
			siteArea = siteAreaIterator.next();

			/*DocumentIdIterator<Content> docContent = myworkspace.contentSearch(
					authoringTmpl, new DocumentId[] { siteArea }, null, null,
					false, false, false, Workspace.SORT_KEY_PUBLISH_DATE, null,
					null, false);*/
			
			DocumentIdIterator<Content> docContent = myworkspace.contentSearch(authoringTmpl,new DocumentId[] { siteArea },null,null,false,false
					,false,false,false, Workspace.SORT_KEY_PUBLISH_DATE,null,null,false,Workspace.WORKFLOWSTATUS_PUBLISHED);

			// scan document

			while (docContent.hasNext() && k <= pageSize * startPage) {
				contentid = docContent.next();
				if (k > pageSize * (startPage - 1)) {
					// khoi tao doi tuong jsonobject
					json = new JSONObject();
					json.put(TOTAL, docContent.getCount());
					// lay tieu de va duong dan cua bai viet
					Content content = myworkspace.getById(contentid);
					String label = content.getTitle();
					json.put(LABEL_DOC, label);
					json.put(MODIFIED_DATE, DateUtil.getDate(
							content.getModifiedDate(),
							DateUtil.SHORT_DATE_PATTERN));
					String url = "";
					if (dir == null || dir.equals("")) {
						url = DETAIL_JSP_WCM_WPS_WCM_CONNECT + library + "/"
								+ library + "/" + site + "/"
								+ content.getName();
						json.put(HREF, url);
					} else {
						url = DETAIL_JSP_WCM_WPS_WCM_CONNECT + library + "/"
								+ library + "/" + dir + "/" + site + "/"
								+ content.getName();
						json.put(HREF, url);
					}
					
					ShortTextComponent soKyHieu = (ShortTextComponent)content.getComponent(SO_KY_HIEU);
					json.put(SO_KY_HIEU, soKyHieu.getText());
					// add vao mang jsonarray
					jsonArray.add(json);
				}
				k++;
			}

			// cau hinh va gui ket qua phan trang
			Pagination pagination = new Pagination();
			PaginationBO paginationBO = new PaginationBO();

			paginationBO.setCurrentPage(startPage);
			paginationBO.setTotalRecord(docContent.getCount());
			paginationBO.setLimit(pageSize);
			paginationBO.setRange(9);
			paginationBO.setLinkFull("group.jsp?GroupId="+site+"&page={page}");
			paginationBO.setLinkFirst("group.jsp?GroupId="+site+"&page=1");

			pagination.init(paginationBO);

			json = new JSONObject();
			json.put("pagination", pagination.html());
			jsonArray.add(json);

			out.println(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
